package ucl.cs.stockBroker.server;

public class PINCipher {

	public static String encryptPin(String pin) {
		char[] encryptedPinArray = new char[pin.length()];
		char[] pinArray = pin.toCharArray();
		int offset = 0;
		for (int i = 0; i < pinArray.length; i++) {
			String stringChar = Character.toString(pinArray[i]);
			Integer intChar = Integer.parseInt(stringChar);
			intChar = intChar + offset;
			if (intChar > 9) {
				intChar = intChar % 10;
			}
			String tempString = intChar.toString();
			encryptedPinArray[i] = tempString.charAt(0);
			offset++;
		}
		String encryptedPin = new String(encryptedPinArray);
		return encryptedPin;
	}

	public static String decryptPin(String encryptedPin) {
		char[] encryptedPinArray = new char[encryptedPin.length()];
		char[] pinArray = encryptedPin.toCharArray();
		int offset = 0;
		for (int i = 0; i < pinArray.length; i++) {
			String stringChar = Character.toString(pinArray[i]);
			Integer intChar = Integer.parseInt(stringChar);
			intChar = intChar - offset;
			if (intChar < 0) {
				intChar = intChar + 10;
			}
			String tempString = intChar.toString();
			encryptedPinArray[i] = tempString.charAt(0);
			offset++;
		}
		String decryptedPin = new String(encryptedPinArray);
		return decryptedPin;
	}
}
